package com.ruoyi.jjkc.jdjh.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.jjkc.jdjh.domain.Jdjh;
import com.ruoyi.jjkc.jdjh.service.IJdjhService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.List;

/**
 * 加计扣除鉴定计划Controller
 *
 * @author huacai
 * @date 2024-12-07
 */
@RestController
@RequestMapping("/jjkc/jdjh")
public class JdjhController extends BaseController {
    @Resource
    private IJdjhService jdjhService;

    /**
     * 查询加计扣除鉴定计划列表
     */
    @PreAuthorize("@ss.hasPermi('jjkc:jdjh:list')")
    @GetMapping("/list")
    public TableDataInfo list(Jdjh jdjh) {
        startPage();
        List<Jdjh> list = jdjhService.selectJdjhList(jdjh);
        return getDataTable(list);
    }

    /**
     * 查询加计扣除鉴定计划列表
     * sfKJjkc 是否可加计扣除 字段为 是
     * sfzSqLb 是否在申请列表 字段为 否
     */
    @GetMapping("/selectJdjhListByJjkcSq")
    public TableDataInfo selectJdjhListByJjkcSq(Jdjh jdjh) {
        startPage();
        List<Jdjh> list = jdjhService.selectJdjhListByJjkcSq(jdjh);
        return getDataTable(list);
    }

    /**
     * 导出加计扣除鉴定计划列表
     */
    @PreAuthorize("@ss.hasPermi('jjkc:jdjh:export')")
    @Log(title = "加计扣除鉴定计划", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Jdjh jdjh) {
        List<Jdjh> list = jdjhService.selectJdjhList(jdjh);
        ExcelUtil<Jdjh> util = new ExcelUtil<Jdjh>(Jdjh.class);
        util.exportExcel(response, list, "加计扣除鉴定计划数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<Jdjh> util = new ExcelUtil<Jdjh>(Jdjh.class);
        util.importTemplateExcel(response, "加计扣除鉴定计划数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "加计扣除鉴定计划", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('jjkc:jdjh:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<Jdjh> util = new ExcelUtil<Jdjh>(Jdjh.class);
        InputStream inputStream = file.getInputStream();
        List<Jdjh> list = util.importExcel(inputStream);
        inputStream.close();
        int count = jdjhService.batchInsertJdjh(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

    /**
     * 获取加计扣除鉴定计划详细信息
     */
    @PreAuthorize("@ss.hasPermi('jjkc:jdjh:query')")
    @GetMapping(value = "/{jdjhId}")
    public AjaxResult getInfo(@PathVariable("jdjhId") Integer jdjhId) {
        return success(jdjhService.selectJdjhByJdjhId(jdjhId));
    }

    /**
     * 新增加计扣除鉴定计划
     */
    @PreAuthorize("@ss.hasPermi('jjkc:jdjh:add')")
    @Log(title = "加计扣除鉴定计划", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Jdjh jdjh) {
        jdjh.setUserId(Math.toIntExact(getUserId())); //用户ID
        jdjh.setDeptId(Math.toIntExact(getDeptId())); //部门ID
        jdjh.setCreateBy(getUsername()); //创建人
        return toAjax(jdjhService.insertJdjh(jdjh));
    }

    /**
     * 修改加计扣除鉴定计划
     */
    @PreAuthorize("@ss.hasPermi('jjkc:jdjh:edit')")
    @Log(title = "加计扣除鉴定计划", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Jdjh jdjh) {
        return toAjax(jdjhService.updateJdjh(jdjh));
    }

    /**
     * 删除加计扣除鉴定计划
     */
    @PreAuthorize("@ss.hasPermi('jjkc:jdjh:remove')")
    @Log(title = "加计扣除鉴定计划", businessType = BusinessType.DELETE)
    @DeleteMapping("/{jdjhIds}")
    public AjaxResult remove(@PathVariable Integer[] jdjhIds) {
        return toAjax(jdjhService.deleteJdjhByJdjhIds(jdjhIds));
    }
}
